home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / usb-creator / install.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.4 KB  |  193 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import getopt
  5. import os
  6. import stat
  7. import sys
  8. import shutil
  9. import subprocess
  10.  
  11. def popen(cmd):
  12.     print >>sys.stderr, str(cmd)
  13.     process = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = sys.stderr, stdin = subprocess.PIPE)
  14.     process.communicate()
  15.     return process
  16.  
  17.  
  18. def main(source, target, persist):
  19.     sys.stdout.write('Copying files\n')
  20.     sys.stdout.flush()
  21.     if not os.path.exists(source) or not os.path.exists(target):
  22.         print >>sys.stderr, 'Source or target does not exist.'
  23.         sys.exit(1)
  24.     
  25.     for dirpath, dirnames, filenames in os.walk(source):
  26.         sp = dirpath[len(source) + 1:]
  27.         for name in dirnames + filenames:
  28.             relpath = os.path.join(sp, name)
  29.             sourcepath = os.path.join(source, relpath)
  30.             targetpath = os.path.join(target, relpath)
  31.             st = os.lstat(sourcepath)
  32.             mode = stat.S_IMODE(st.st_mode)
  33.             if stat.S_ISLNK(st.st_mode):
  34.                 if os.path.lexists(targetpath):
  35.                     os.unlink(targetpath)
  36.                 
  37.                 linkto = os.readlink(sourcepath)
  38.                 sys.stderr.write('Tried to symlink %s -> %s\n' % (linkto, targetpath))
  39.                 continue
  40.             if stat.S_ISDIR(st.st_mode):
  41.                 if not os.path.isdir(targetpath):
  42.                     os.mkdir(targetpath, mode)
  43.                 
  44.             os.path.isdir(targetpath)
  45.             if stat.S_ISCHR(st.st_mode):
  46.                 os.mknod(targetpath, stat.S_IFCHR | mode, st.st_rdev)
  47.                 continue
  48.             if stat.S_ISBLK(st.st_mode):
  49.                 os.mknod(targetpath, stat.S_IFBLK | mode, st.st_rdev)
  50.                 continue
  51.             if stat.S_ISFIFO(st.st_mode):
  52.                 os.mknod(targetpath, stat.S_IFIFO | mode)
  53.                 continue
  54.             if stat.S_ISSOCK(st.st_mode):
  55.                 os.mknod(targetpath, stat.S_IFSOCK | mode)
  56.                 continue
  57.             if stat.S_ISREG(st.st_mode):
  58.                 if os.path.exists(targetpath):
  59.                     os.unlink(targetpath)
  60.                 
  61.                 fail = False
  62.                 
  63.                 try:
  64.                     sourcefh = open(sourcepath, 'rb')
  65.                     targetfh = open(targetpath, 'wb')
  66.                     
  67.                     try:
  68.                         shutil.copyfileobj(sourcefh, targetfh)
  69.                     except Exception:
  70.                         e = None
  71.                         fail = True
  72.                         print >>sys.stderr, str(e) + '\n'
  73.  
  74.                 finally:
  75.                     sourcefh.close()
  76.                     targetfh.close()
  77.                     if fail:
  78.                         sys.exit(1)
  79.                     
  80.  
  81.                 continue
  82.         
  83.     
  84.     popen([
  85.         'rm',
  86.         '-rf',
  87.         '%s/syslinux' % target])
  88.     popen([
  89.         'mv',
  90.         '%s/isolinux' % target,
  91.         '%s/syslinux' % target])
  92.     popen([
  93.         'mv',
  94.         '%s/syslinux/isolinux.cfg' % target,
  95.         '%s/syslinux/syslinux.cfg' % target])
  96.     for filename in [
  97.         'syslinux/syslinux.cfg',
  98.         'syslinux/text.cfg']:
  99.         f = None
  100.         
  101.         try:
  102.             f = open(os.path.join(target, filename), 'r')
  103.             label = ''
  104.             to_write = []
  105.             for line in f.readlines():
  106.                 line = line.strip('\n').split(' ')
  107.                 for l in line:
  108.                     if l:
  109.                         command = l
  110.                         break
  111.                         continue
  112.                 
  113.                 if command.lower() == 'append':
  114.                     pos = line.index(command) + 2
  115.                     if label not in ('check', 'memtest', 'hd'):
  116.                         if persist != '0':
  117.                             line.insert(pos, 'persistent')
  118.                         
  119.                         line.insert(pos, 'cdrom-detect/try-usb=true')
  120.                     
  121.                     if label not in ('memtest', 'hd'):
  122.                         line.insert(pos, 'noprompt')
  123.                     
  124.                 elif command.lower() == 'label':
  125.                     label = line[1].strip()
  126.                 
  127.                 to_write.append(' '.join(line) + '\n')
  128.             
  129.             f.close()
  130.             f = open(os.path.join(target, filename), 'w')
  131.             f.writelines(to_write)
  132.         except Exception:
  133.             e = None
  134.             print >>sys.stderr, str(e) + '\n'
  135.             print >>sys.stderr, 'Unable to add persistence to the configuration (%s)\n' % filename
  136.         finally:
  137.             if f:
  138.                 f.close()
  139.             
  140.  
  141.     
  142.     popen([
  143.         'cp',
  144.         '%s/syslinux/syslinux.cfg' % target,
  145.         '%s/syslinux.cfg' % target])
  146.     if persist != '0':
  147.         sys.stdout.write('Creating persistence file\n')
  148.         sys.stdout.flush()
  149.         popen([
  150.             'dd',
  151.             'if=/dev/zero',
  152.             'bs=%s' % persist,
  153.             'of=%s/casper-rw' % target,
  154.             'count=1'])
  155.         sys.stdout.write('Making persistence filesystem\n')
  156.         sys.stdout.flush()
  157.         popen([
  158.             'mkfs.ext3',
  159.             '-F',
  160.             '%s/casper-rw' % target])
  161.     
  162.     popen([
  163.         'sync'])
  164.  
  165. if __name__ == '__main__':
  166.     source = ''
  167.     target = ''
  168.     persist = 0
  169.     
  170.     try:
  171.         (opts, args) = getopt.getopt(sys.argv[1:], 's:t:p:')
  172.     except getopt.GetoptError:
  173.         sys.exit(1)
  174.  
  175.     for opt, arg in opts:
  176.         if opt == '-s':
  177.             source = arg
  178.             continue
  179.         if opt == '-t':
  180.             target = arg
  181.             continue
  182.         if opt == '-p':
  183.             persist = arg
  184.             continue
  185.     
  186.     if source and target:
  187.         main(source, target, persist)
  188.         sys.exit(0)
  189.     else:
  190.         print >>sys.stderr, 'Source or target device not specified.  Cannot continue.\n'
  191.         sys.exit(1)
  192.  
  193.